/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.casestudy1.view;

import com.casestudy1.main.MainMidlet;
import com.nokia.mid.location.LocationUtil;
import com.sun.lwuit.Button;
import com.sun.lwuit.Command;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import javax.microedition.location.AddressInfo;
import javax.microedition.location.Location;
import javax.microedition.location.LocationException;
import javax.microedition.location.LocationProvider;
import javax.microedition.location.QualifiedCoordinates;

/**
 *
 * @author Lenovo
 */
public class HistoryTravelForm extends View implements Runnable {

    AddressInfo address;
    LocationProvider provider;
    Location location;
    QualifiedCoordinates coordinates;
    Thread t;
    private Button btnSendCellID;
    private Button btnListHistory;
    private Command cmdBack;
    double latitude;
    double longitude;
    String addressname;

    public HistoryTravelForm(final MainMidlet midlet) {
        super(midlet);
        addComponent(btnSendCellID);
        addComponent(btnListHistory);

        btnSendCellID.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                t = new Thread();
                t.start();
                //send cellid

            }
        });

        btnListHistory.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
//                ListCellIDHistory list = new ListCellIDHistory();
                removeAll();
//                addComponent(list);
                addCommand(cmdBack);
            }
        });

        cmdBack = new Command("") {
            public void actionPerformed(ActionEvent e) {
                midlet.showMainMenu();
            }
        };
        setBackCommand(cmdBack);
        addCommand(cmdBack);

    }

    public void run() {
        try {
            //Specify the retrieval method to Online/Cell-ID
            int[] methods = {(Location.MTA_ASSISTED | Location.MTE_CELLID | Location.MTE_SHORTRANGE | Location.MTY_NETWORKBASED)};
            try {
                // Retrieve the location provider
                provider = LocationUtil.getLocationProvider(methods, null);
            } catch (SecurityException ex) {
                ex.printStackTrace();
            } catch (LocationException ex) {
                ex.printStackTrace();
            }
            location = provider.getLocation(50000);
            coordinates = location.getQualifiedCoordinates();
            address = location.getAddressInfo();
            latitude = coordinates.getLatitude();
            longitude = coordinates.getLongitude();
            addressname = address.getField(AddressInfo.DISTRICT);
            Dialog.show("Info", "Address:" + addressname + "\nLatitude:" + latitude + "\nLongitude:" + longitude, "OK", null);

        } catch (NoClassDefFoundError ex) {

            Dialog.show("Info", "Cell-ID retrieval not supported on this device", "OK", null);

        } catch (InterruptedException e) {
            Dialog.show("Info", "Location retrieval was interrupted", "OK", null);

        } catch (LocationException x) {
            Dialog.show("Info", "Location could not be retreived", "OK", null);
        }
    }
}
